package smecta.client.print.controller.product;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.ComboBoxTableCell;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import smecta.client.print.domain.ScpProduct;
import smecta.client.print.domain.ScpProductPropertyValuelist;
import smecta.client.print.domain.ScpProductSpecs;
import smecta.client.print.mapper.ScpProductMapper;
import smecta.client.print.mapper.ScpProductPropertyValuelistMapper;
import smecta.client.print.mapper.ScpProductSpecsMapper;

import java.util.HashMap;
import java.util.List;

@Component
public class ProductController {

    @Autowired
    private ScpProductPropertyValuelistMapper scpProductPropertyValuelistMapper;

    @Autowired
    private ScpProductMapper scpProductMapper;

    @Autowired
    private ScpProductSpecsMapper scpProductSpecsMapper;

    @FXML
    private TableView<ScpProduct> table;

    @FXML
    private TableColumn<ScpProduct, String> col0;

    @FXML
    private TableColumn<ScpProduct, String> col1;

    @FXML
    private TableColumn<ScpProduct, String> col2;

    @FXML
    private TableColumn<ScpProduct, String> col3;

    @FXML
    private TableColumn<ScpProduct, String> col4;

    public void manualInit() {
        // 初始化数据
        List<ScpProduct> list = scpProductMapper.selectAll();
        // 可以添加更多数据...
        table.getItems().clear();
        table.getItems().addAll(list);
    }

    // 属性 枚举值
    private HashMap<String, ObservableList<String>> attrEnumMap = new HashMap<>();

    private String propertyCode001;

    private String propertyCode002;

    private String propertyCode003;

    private String propertyCode004;

    @FXML
    public void initialize() {
        initColumnEnum();
        table.setEditable(true); // 使表格可编辑

        col0.setCellValueFactory(new PropertyValueFactory<>("id"));

        col1.setCellValueFactory(new PropertyValueFactory<>("pinming"));
        col1.setCellFactory(ComboBoxTableCell.forTableColumn(attrEnumMap.get("pinming")));
        col1.setOnEditCommit(
                event -> event.getTableView().getItems().get(
                        event.getTablePosition().getRow()).setPinming(event.getNewValue())
        );

        col2.setCellValueFactory(new PropertyValueFactory<>("guige"));
        col2.setCellFactory(ComboBoxTableCell.forTableColumn(attrEnumMap.get("guige")));
        col2.setOnEditCommit(
                event -> event.getTableView().getItems().get(
                        event.getTablePosition().getRow()).setGuige(event.getNewValue())
        );

        col3.setCellValueFactory(new PropertyValueFactory<>("seze"));
        col3.setCellFactory(ComboBoxTableCell.forTableColumn(attrEnumMap.get("seze")));
        col3.setOnEditCommit(
                event -> event.getTableView().getItems().get(
                        event.getTablePosition().getRow()).setSeze(event.getNewValue())
        );

        col4.setCellValueFactory(new PropertyValueFactory<>("fenlei"));
        col4.setCellFactory(ComboBoxTableCell.forTableColumn(attrEnumMap.get("leixing")));
        col4.setOnEditCommit(
                event -> event.getTableView().getItems().get(
                        event.getTablePosition().getRow()).setFenlei(event.getNewValue())
        );
    }

    private void initColumnEnum() {
        attrEnumMap.clear();
        List<ScpProductPropertyValuelist> all = scpProductPropertyValuelistMapper.selectScpProductPropertyValuelistList(new ScpProductPropertyValuelist());
        for (ScpProductPropertyValuelist item : all) {
            String key = item.getCode();
            String value = item.getValue();
            if (!attrEnumMap.containsKey(key)) {
                attrEnumMap.put(key, FXCollections.observableArrayList());
            }
            attrEnumMap.get(key).add(value);
        }

        List<ScpProductSpecs> specs = scpProductSpecsMapper.selectAll();
        for (ScpProductSpecs item : specs) {
            String key = "guige";
            String value = item.getGuige();
            if (!attrEnumMap.containsKey(key)) {
                attrEnumMap.put(key, FXCollections.observableArrayList());
            }
            attrEnumMap.get(key).add(value);
        }
    }

    public void add() {
        table.getItems().add(new ScpProduct("请选择","请选择","请选择","请选择"));
        table.refresh(); // 刷新表格显示新数据
    }

    public void del() {
        table.getItems().remove(table.getItems().size() - 1);
        table.refresh(); // 刷新表格显示新数据
    }

    public void save() {
        List<ScpProduct> all = scpProductMapper.selectAll();
        for (int i = 0; i < all.size(); i++) {
            scpProductMapper.deleteByPrimaryKey(all.get(i).getId());
        }

        table.getItems().forEach(item -> {
            List<ScpProduct> query = scpProductMapper.select(item);
            if (CollectionUtils.isEmpty(query)) {
                scpProductMapper.insertSelective(item);
            }
        });
    }
}